package com.example.walletdemo.service.impl;

import com.example.walletdemo.entity.Bank;
import com.example.walletdemo.entity.Order;
import com.example.walletdemo.entity.Record;
import com.example.walletdemo.entity.Wallet;
import com.example.walletdemo.mapper.WalletMapper;
import com.example.walletdemo.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WalletServiceImpl implements WalletService {

    @Autowired
    private WalletMapper walletMapper;


    @Override
    public Wallet selectWalletByUserName(String username) {
        return walletMapper.selectWalletByUserName(username);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertOrder(Order order,Wallet wallet) {



            if (walletMapper.insertOrder(order) > 0){


               double balance = wallet.getBalance();

               wallet.setBalance(balance-order.getMoney());
               wallet.setUpdateTime(order.getCreateTime());


                walletMapper.updateWallet(wallet);

                return true;
            }else {
                return false;
            }

    }

    @Override
    public Wallet selectWalletById(int wid) {
        return walletMapper.selectWalletById(wid);
    }

    @Override
    public int updateWallet(Wallet wallet) {
        return walletMapper.updateWallet(wallet);
    }

    @Override
    public void insertRecord(Record record) {
        walletMapper.insertRecord(record);
    }

    @Override
    public Order selectOrdersByOid(int oid) {
        return walletMapper.selectOrdersByOid(oid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean refund(Order orders) {

        orders.setStatus("2");

        if (walletMapper.updateOrders(orders)){

            Wallet wallet = new Wallet();

            wallet = walletMapper.selectWalletById(orders.getWid());

            wallet.setUpdateTime(orders.getUpdateTime());

            double balance = wallet.getBalance()+orders.getMoney();
            wallet.setBalance(balance);

            walletMapper.updateWallet(wallet);

            return true;
        }


        return false;
    }

    @Override
    public List<Record> selectRecordByWid(int wid) {
        return walletMapper.selectRecordByWid(wid);
    }

    @Override
    public Bank selectBankByCardNo(String cardNo) {
        return walletMapper.selectBankByCardNo(cardNo);
    }

    @Override
    public Wallet selectWalletBycardNo(String cardNo) {
        return walletMapper.selectWalletBycardNo(cardNo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean reflect(Wallet wallet, String cardNo, double money) {

        Bank bank = new Bank();

        bank = walletMapper.selectBankByCardNo(cardNo);

        double wBalance = wallet.getBalance()-money;

        double bBalance = bank.getBalance()+money;

        wallet.setBalance(wBalance);

        bank.setBalance(bBalance);
        bank.setUpdateTime(wallet.getUpdateTime());

        if (walletMapper.updateWallet(wallet) > 0){

            walletMapper.updateBank(bank);

            return true;
        }

        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean recharge(Bank bank, double money) {


        Wallet wallet = new Wallet();

        wallet = walletMapper.selectWalletById(bank.getWid());

        double bBalance = bank.getBalance()-money;

        double wBalance = wallet.getBalance()+money;

        bank.setBalance(bBalance);

        wallet.setBalance(wBalance);
        wallet.setUpdateTime(bank.getUpdateTime());



        if (walletMapper.updateBank(bank)){


            walletMapper.updateWallet(wallet);

            return true;


        }




        return false;
    }

}
